{% extends 'subpage/base.html' %}
{% block title_text %}
    模板管理
{% endblock %}
{% block scripts %}
    {{ super() }}
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.config.js') }}"></script>
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.all.min.js') }}"></script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/My97DatePicker/WdatePicker.js") }}"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/lang/zh-cn/zh-cn.js') }}"></script>
    <script src="{{ url_for('static',filename='ueditor/ueditor.parse.js') }}"></script>
    <script>
        setTimeout(function () {
            uParse('div',
                {
                    'highlightJsUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCore.js") }}',
                    'highlightCssUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCoreDefault.css") }}'

                })
        }, 300);
    </script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/myAlert/myAlert.min.js") }}"></script>
{% endblock %}
{% block ueditorbox %}
    <div class="form-group clearfix">
        <label for="selectDepart" class="control-label col-xs-2">选择部门</label>
        <div class="col-xs-8">
            <select name="" id="selectDepart" class="form-control">
            </select>
        </div>
    </div>
    <div class="clearfix">
        <script id="editor3" type="text/plain"></script>
    </div>
    <div class="clearfix submit-box">
        <button type="button" class="btn btn-primary btn-confirm pull-right">提交</button>
    </div>
{% endblock %}
{% block content %}
    {{ super() }}
    <script>
        $(function () {
            var ue = UE.getEditor('editor3', {
                serverUrl: "/upload",
                initialFrameWidth: 890,  //初始化编辑器宽度,默认1000
                initialFrameHeight: 500
            });
            loadDepartLists();
            function loadDepartLists() {
                ajaxRequest('GET', '/groups', {"viewDetail": 1}, success, error, '{{ csrf_token }}');
                function success(data) {
                    var str = '<option value="" data-departId="none" class="form-control" selected="selected">--请选择--</option><optgroup label="部门">',
                        str2 = '<optgroup label="其他">';
                    for (var i = 0; i < data.length; i++) {
                        var item = data[i];
                        if (item.departName === 'ADMIN') {
                            str2 += '<option class="form-control" data-departId="' + item.departId + '"  data-departname="' + item.departName + '" data-departAlias="' + item.departAlias + '">帮助手册</option></optgroup>';
                        } else {
                            str += '<option class="form-control" data-departId="' + item.departId + '"  data-departname="' + item.departName + '" data-departAlias="' + item.departAlias + '">' + item.departAlias + '</option>';
                        }
                    }
                    str += '</optgroup>';
                    $('#selectDepart').html(str + str2);
                }

                function error(error) {
                    $.myAlert({title: '出错提示', message: '请求出错!'});
                }
            }

            $('#selectDepart').change(function () {
                var data = {
                    "departId": $('#selectDepart option:selected').attr('data-departId')
                };
                var departName = $('#selectDepart option:selected').attr('data-departname');
                if (data.departId !== "none") {
                    ajaxRequest('GET', '/gettemplate', data, success, error, '{{ csrf_token }}');
                } else {
                    ue.setContent("");
                }
                function success(data) {
                    if (data.code === 0) {
                        ue.setContent(data.data.tempText);
                    } else {
                        $.myAlert({title: '出错提示', message: data.errmessage});
                    }

                }

                function error() {
                    $.myAlert({title: '出错提示', message: '请求出错!'});
                }
            });
            $('.btn-confirm').click(function () {
                var departId = $('#selectDepart option:selected').attr('data-departId'),
                    departAlias = $('#selectDepart option:selected').attr('data-departAlias'),
                    departName = $('#selectDepart option:selected').attr('data-departname');//string
                if (departId && departId !== "none") {
                    var data = {}, content = ue.getContent();
                    data = {
                        departId: departId,
                        tempText: content
                    };
                    if (content.length === 0) {
                        $.myAlert({title: "出错提示", message: "请完善富文本信息！"});
                        return;
                    }
                    function success(data) {
                        console.log(data);
                        if (data.code === 0) {
                            $.myConfirm({
                                title: '信息提示', message: '成功保存模板,快去试一试吧!', callback: function () {
                                    switch (departName) {
                                        case 'OP'://系统运维部
                                            window.location.href = '/sysmain?departId=' + departId + '&departAlias=' + departAlias + '&departName=' + departName;
                                            break;
                                        case 'SEC'://网络安全部
                                            window.location.href = '/netsafe?departId=' + departId + '&departAlias=' + departAlias + '&departName=' + departName;
                                            break;
                                        case 'ADMIN'://系统管理员->帮助手册
                                            window.location.href = '/gethelp?departId=' + departId + '&departAlias=' + departAlias + '&departName=' + departName;
                                            break;
                                        default:
                                            window.location.href = '/common?departId=' + departId + '&departAlias=' + departAlias + '&departName=' + departName;
                                            break;
                                    }
                                }
                            });

                        } else if (data.code === 1) {
                            $.myAlert({title: "出错提示", message: data.errmessage});
                        }

                    }

                    function error(ero) {
                        $.myAlert({title: '出错提示', message: '请求出错!'});
                    }

                    ajaxRequest('POST', '/edittemplate', data, success, error, '{{ csrf_token }}');
                } else {
                    $.myAlert({title: '出错提示', message: '请选择相应的部门!'});
                }
            });
        });
    </script>
{% endblock %}